home *** CD-ROM | disk | FTP | other *** search
- Path: quadostimpy.natinst.com!user
- From: rcauvin@natinst.com (Roger L. Cauvin)
- Newsgroups: comp.lang.c++
- Subject: Re: should operator== and operator= be virtual?
- Date: 1 Feb 1996 15:02:06 GMT
- Organization: National Instruments
- Message-ID: <rcauvin-0102960906490001@quadostimpy.natinst.com>
- References: <4ejvkr$btb@itnews.sc.intel.com>
- NNTP-Posting-Host: quadostimpy.natinst.com
-
- In article <4ejvkr$btb@itnews.sc.intel.com>, etse@scdt.intel.com (Eric
- Tse) wrote:
-
- > Hi,
- >
- > Should operator==() and operator=() of a class be virtual if the class is
- > planned for extensibility? Personally I think in general assignement
- > operators should be non-virtual, but I am not sure about equality operator.
- > Could anyone helps? Thanks.
-
- The following example illustrates the benefits of both virtual assignment
- operators and virtual comparison operators:
-
- // Abstract string class. Derived classes may use any strategy
- // they wish for character storage.
- class String
- {
- public:
- virtual ~String(void) = 0;
- virtual String & operator=(const char *cString) = 0;
- virtual String & operator=(const String &string) = 0;
- virtual bool operator==(const String &string) const = 0;
-
- protected:
- String(void);
- };
-
- // Concrete static string class that uses a static array to
- // store its characters.
- class StaticString : public String
- {
- public:
- StaticString(void);
- virtual ~StaticString(void);
- virtual String & operator=(const char *cString);
- virtual String & operator=(const String &string);
- virtual StaticString & operator=(const StaticString &staticString);
- virtual bool operator==(const String &string) const = 0;
-
- private:
- char staticBuffer[256];
- };
-
- // Concrete dynamic string class that uses a dynamic buffer
- // for storage of its characters.
- class DynamicString : public String
- {
- public:
- DynamicString(void);
- virtual ~DynamicString(void);
- virtual String & operator=(const char *cString);
- virtual String & operator=(const String &string);
- virtual DynamicString & operator=(const DynamicString &dynamicString);
- virtual bool operator==(const String &string) const = 0;
-
- private:
- char *dynamicBuffer;
- };
-
- // Given a reference to string, set it equal to today's date.
- void GetDateString(String &dateString)
- {
- dateString = "Thursday, February 1, 1996";
- }
-
- // Given to string references, compare them for equality.
- bool CompareDateStrings(const String &dateString1, const String &dateString2)
- {
- return dateString1 ==dateString2;
- }
-
- A deep inheritance structure containing many abstract classes MUST use
- virtual assignment and comparison operators in order to be sufficiently
- polymorphic.
-
- ---
-
- Roger L. Cauvin
- rcauvin@natinst.com
- Software Engineer
- National Instruments
-